﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZX.BLL.Extend;
using ZX.SYS.Library;
using ZX.EHR.Library;
using ZX.Common;
using ZX.Common.Extend;
using ZX.DBModule;

//==================================================================
// Copyright (C) 2011 深圳市中旭企业管理股份有限公司
// 作 者：it012
// Email: 
// 日 期：2012/3/28
// 描 述：员工基本信息业务处理
// 版 本：1.0.0.0    
//    
//==================================================================

namespace ZX.BLL
{
    public class BEmployee : BaseBussnies
    {
       
        public hrEmployee Employee
        {
            get
            {
                hrEmployee employee = this.entity as hrEmployee;
                if (employee == null)
                    throw new ArgumentNullException("this.entity---employee");
                return employee;
            }
        }

        #region
        //public override bool Insert()
        //{
        //    bool bl = base.Insert();
        //    if (bl)
        //    {
        //        CreateUser();
        //    }
        //    return bl;
        //}

        //public override bool Insert(object entitys)
        //{
        //    bool bl =  base.Insert(entitys);
        //    if (bl)
        //    {
        //        CreateUser();
        //    }
        //    return bl;
        //}
        public hrEmployee getHrEmployee(Guid empId)
        {
            return this.GetEntityById<hrEmployee>(empId);
        }
        private void CreateUser()
        {
            DatUser user = new DatUser();
            user.UserID = Guid.NewGuid();
            user.LoginName = Employee.EmployeeCode;
            user.UName = Employee.CName;
            user.Password = Employee.IDCardNO.Substring(Employee.IDCardNO.Length - 6).GetMd5Hash();
            user.EmpID = Employee.ID;
            user.Email = Employee.Email;
            user.AddUser = Employee.AddUser;
            user.EffectDate = System.DateTime.Now;
            user.AddTime = DateTime.Now;
            user.ExpireDate = System.DateTime.Now.AddYears(30);
            user.IsOutsite = false;
            base.entity = user;
            base.Insert();
        }

        public override bool Insert(object entitys, out string msg)
        {
            bool bl = base.Insert(entitys, out msg);
            //手工录入 2014-10-08 15:54:59
            if (bl)
            {
                hrEmployee emp = (hrEmployee)entitys;
                //创建用户账户
                DatUser user = new DatUser();
                user.UserID = Guid.NewGuid();
                user.LoginName = emp.EmployeeCode;
                user.UName = emp.CName;
                user.Password = emp.IDCardNO.Substring(emp.IDCardNO.Length - 6).GetMd5Hash();
                user.EmpID = emp.ID;
                user.Email = emp.Email;
                user.AddUser = emp.AddUser;
                user.EffectDate = System.DateTime.Now;
                user.AddTime = DateTime.Now;
                user.ExpireDate = System.DateTime.Now.AddYears(30);
                user.IsOutsite = false;
                BaseBussnies userBll = new BaseBussnies(user);
                try
                {
                    userBll.Insert();
                }
                catch { }
                //新员工入职将信息写入消息列队
                DatPost empPost = new DatPost();
                BaseBussnies bll = new BaseBussnies(empPost);
                empPost = bll.GetEntityByCondition("PostID='" + emp.PostID + "'") as DatPost;//当前岗位
                //DatMessageQueue message = new DatMessageQueue();
                //message.MsgID = Guid.NewGuid();
                //message.Title = "欢迎新员工——" + empPost.PostName + emp.CName;
                //message.MessageType = "h";//标示是HR的消息
                ////获取岗位名称
                //DatPost post = this.GetEntityByCondition<DatPost>("PostID='" + emp.PostID + "'");
                //string postName = string.Empty;
                //if (post != null) postName = post.PostName;
                //message.MsgContent = "亲爱的AKD伙伴：br大家好! br公司新进了一位伙伴span1 " + emp.CName + "span2---现任span1" + postName + "span2。br首先代表公司热烈欢迎新伙伴的加入，希望大家在工作中能帮助新伙伴尽快适应我们的工作环境，最后，希望大家在今后的工作中一起快乐学习！快乐工作！快乐生活！！";
                //message.ExpectTime = DateTime.Now;
                //message.PushTime = DateTime.Now;
                //message.PushState = "0";//状态为未发送
                //message.AddTime = DateTime.Now;
                //message.AddUser = emp.AddUser;
                //bll = new BaseBussnies(message);
                //bll.Insert();
                ////写接收者
                //DatMessageReceiver receiver = new DatMessageReceiver();
                //receiver.MsItemID = Guid.NewGuid();
                //receiver.MsgID = message.MsgID;
                //receiver.ReceiverType = "u";
                //receiver.ReceiverID = Guid.Empty;//公共消息，接收者为全体
                //bll = new BaseBussnies(receiver);
                //bll.Insert();
                ////还要给入职者本人首次进入EHR时一条消息
                //message.MsgID = Guid.NewGuid();
                //message.Title = "欢迎您，" + emp.CName;
                //message.MessageType = "h";//标示是HR的消息
                //message.MsgContent = "span1" + emp.CName + "span2伙伴：br您好！ br欢迎您加入AKD这个大家庭，在这里首先要与您一起分享公司的核心价值观：团队精神，敬畏客户，勇于负责，持续成长！这是在公司做事的基本原则和底线，我们共勉！在以后的工作中如果遇到困难需要帮助可以随时与其他伙伴联系，愿您在以后的工作中快乐学习！快乐工作！快乐生活！";
                //message.ExpectTime = DateTime.Now;
                //message.PushTime = DateTime.Now;
                //message.PushState = "0";//状态为未发送
                //message.AddTime = DateTime.Now;
                //message.AddUser = emp.AddUser;
                //bll = new BaseBussnies(message);
                //bll.Insert();
                //receiver.MsItemID = Guid.NewGuid();
                //receiver.MsgID = message.MsgID;
                //receiver.ReceiverType = "u";
                //receiver.ReceiverID = emp.ID;//接收者为入职者
                //bll = new BaseBussnies(receiver);
                //bll.Insert();
                ////对于第七职级上员工入职要给员工关系专员提醒
                ////获取该员工岗位
                try
                {
                //    //获取职位职级
                //    DatPhraseDetail datDetail = new DatPhraseDetail();
                //    bll = new BaseBussnies(datDetail);
                //    datDetail = bll.GetEntityByCondition("PhraseID ='" + empPost.PostRank + "'") as DatPhraseDetail;
                //    if (int.Parse(datDetail.PhraseValue) < 8)//七级以上级别才提醒
                //    {
                //        //将消息写入消息池
                //        DatMessageQueue postChangemessage = new DatMessageQueue();
                //        postChangemessage.MsgID = Guid.NewGuid();
                //        //postChangemessage.Title = emp.CName + "入职发文提醒";
                //        postChangemessage.Title = "欢迎新员工——" + emp.CName;
                //        postChangemessage.MessageType = "h";//标示是HR的消息
                //        //postChangemessage.MsgContent = "br span1" + emp.CName + "span2伙伴，已于span1" + emp.JoinDate.ToString("yyyy年MM月dd日") + "span2办理完毕入职手续，出任span1" + empPost.PostName + "span2，请跟进发文工作。";
                //        postChangemessage.MsgContent = "亲爱的AKD伙伴：br大家好! br公司新进了一位伙伴span1 " + emp.CName + "span2---现任span1" + postName + "span2。br首先代表公司热烈欢迎新伙伴的加入，希望大家在工作中能帮助新伙伴尽快适应我们的工作环境，最后，希望大家在今后的工作中一起快乐学习！快乐工作！快乐生活！！";
                //        postChangemessage.ExpectTime = DateTime.Now;
                //        postChangemessage.PushTime = DateTime.Now;
                //        postChangemessage.PushState = "0";//状态为未发送
                //        postChangemessage.AddTime = DateTime.Now;
                //        postChangemessage.AddUser = emp.AddUser;
                //        bll = new BaseBussnies(postChangemessage);
                //        bll.Insert();
                //        //获取员工关系专员
                //        hrEmployee hrEmp = new hrEmployee();
                //        bll = new BaseBussnies(hrEmp);
                //        IList<BaseEntity> list_Emp = bll.GetEntitysByCondition("PostID=(select PostID from DatPost where PostName like '%员工关系%专员')");
                //        foreach (BaseEntity entity in list_Emp)//给每个关系专员提醒
                //        {
                //            hrEmployee tempHrEmp = entity as hrEmployee;
                //            DatMessageReceiver hrReceiver = new DatMessageReceiver();
                //            hrReceiver.MsItemID = Guid.NewGuid();
                //            hrReceiver.MsgID = postChangemessage.MsgID;
                //            hrReceiver.ReceiverType = "u";
                //            hrReceiver.ReceiverID = tempHrEmp.ID;//接收者为员工关系专员
                //            bll = new BaseBussnies(hrReceiver);
                //            bll.Insert();
                //        }
                //    }

                    //级给用户增加一个EHR公共角色
                    SysRole sysRole = new SysRole();
                    bll = new BaseBussnies(sysRole);
                    sysRole = bll.GetEntityByCondition("RoleName='EHR公共角色'") as SysRole;
                    if (sysRole != null)
                    {
                        SysUserRole sysUserRole = new SysUserRole();
                        sysUserRole.UserRoleID = Guid.NewGuid();
                        sysUserRole.UserID = user.UserID;
                        sysUserRole.RoleID = sysRole.RoleID;
                        sysUserRole.Remark = "ehr";
                        bll = new BaseBussnies(sysUserRole);
                        bll.Insert();
                        //将该用户桌面员工提醒设置显示
                        SysRoleDesktop sysRoleDeskTop = new SysRoleDesktop();
                        bll = new BaseBussnies(sysRoleDeskTop);
                        sysRoleDeskTop = bll.GetEntityByCondition("DeskTopName='员工提醒' and RoleId='" + sysRole.RoleID + "'") as SysRoleDesktop;
                        if (sysRoleDeskTop != null)
                        {
                            SysUserDesktop sysUserDeskTop = new SysUserDesktop();
                            sysUserDeskTop.UserDesktopID = Guid.NewGuid();
                            sysUserDeskTop.RoleDesktopID = sysRoleDeskTop.RoleDesktopID;
                            sysUserDeskTop.Region = "right";
                            sysUserDeskTop.Sort = 0;
                            sysUserDeskTop.AddTime = DateTime.Now;
                            sysUserDeskTop.AddUser = user.UserID;
                            bll = new BaseBussnies(sysUserDeskTop);
                            bll.Insert();
                        }
                    }
                }
                catch
                { }
            }
            return bl;
        }

        //public override bool Insert(out string msg)
        //{
        //    bool bl = base.Insert(out msg);
        //    if (bl)
        //    {
        //        CreateUser();
        //    }
        //    return bl;
        //}
        #endregion

        public override string CheckData()
        {
            IList<string> list = new List<string>();     

            string result = string.Empty;
            //if (false == Employee.CName.IsCharacter(20))
            //{
            //    result = "姓名必须是汉字,并且字数不能超过20个;";
            //    list.Add(result);                
            //}

            if (false == Employee.IDCardNO.IsIDCardNO())
            {
                result = "身份证必须是15位或者18位,并且由数字或者数字和x字符组成;";
                //list.Add(result);
            }
            else
            {
                if (true == string.IsNullOrEmpty(Employee.ID.ToString()))
                {
                    result = "身份证号不能为空;";
                    list.Add(result);
                }
                else
                {
                    //检查当前Employee的IDCardNO是否被其它人使用
                    hrEmployee emp = this.GetEntityByCondition<hrEmployee>("ID!='" + Employee.ID.ToString() + "' and IDCardNO='" + Employee.IDCardNO + "'");
                    if (emp != null)
                    {
                        result = "身份证号已经存在,该员工基本资料已存在;";
                        if (emp.EmployeeStatus == 3) //离职人员
                        {
                            result = "此人信息已存在于离职库中，属再次入职人员。请在离职库中做再次入职操作!";
                        }
                        list.Add(result);
                    }
                }
            }
            
            //if (false == Employee.PhoneNo.IsMobilePhoneNO())
            //{
            //    result = "手机号码由11位数字组成;";
            //    list.Add(result);
            //}

            if (!string.IsNullOrEmpty(Employee.Email))
            {
                if (false == Employee.Email.IsEmail())
                {
                    result = "Email格式类似为:name@domain.com;";
                    list.Add(result);
                }
            }

            if (list.Count > 0)
            {
                return Common.Globals.getJSON<string>(list);
            }
            else
            {
                return "";
            }
        }
    }
}
